#include <func.h>

int main(int argc,char* argv[])
{
    ARGS_CHECK(argc,3)
    int fdr,fdw;
    fdr=open(argv[1],O_RDONLY);
    ERROR_CHECK(fdr,-1,"open1");
    fdw=open(argv[2],O_WRONLY);
    ERROR_CHECK(fdw,-1,"open2");
    char buf[128]={0};
    fd_set rdset;
    int ret;
    int rd_num;//记录是哪个文件描述符里有数据
    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(fdr,&rdset);//将fdr与标准输入的文件描述符写入rdset数组
        rd_num=select(fdr+1,&rdset,NULL,NULL,NULL);//当数组中的某个
        if(FD_ISSET(STDIN_FILENO,&rdset))
        {
            memset(buf,0,sizeof(buf));
            ret=read(STDIN_FILENO,buf,sizeof(buf));
            if(0==ret)
            {
                printf("see you tomorrow\n");
                break;
            }
            write(fdw,buf,strlen(buf)-1);
        }
        if(FD_ISSET(fdr,&rdset))
        {
            memset(buf,0,sizeof(buf));
            ret=read(fdr,buf,sizeof(buf));
            if(0==ret)
            {
                printf("bye\n");
                break;
            }
            printf("%s\n",buf);
        }
    }
    close(fdr);
    close(fdw);
    return 0;
}